package org.stimuliband.dsp.goertzel;

public class Goertzel {

    final Coefficients coeff;

    public Goertzel(Coefficients coeff) {
        this.coeff = coeff;
    }

    public double magnitude(double[] signal, int forFrequence) {

        double current = 0.0;
        double previous1 = 0.0;
        double previous2 = 0.0;

        for (int n = 0; n < signal.length; n++) {
            current = (double) signal[n] - previous2 + coeff.coefficient(forFrequence) * previous1;
            previous2 = previous1;
            previous1 = current;
        }

        double magnitude = (previous2 * previous2) + (previous1 * previous1) - coeff.coefficient(forFrequence) * previous1 * previous2;
        magnitude = Math.sqrt(magnitude);

        return magnitude;
    }
}
